package com.tqjc.common.ScheduledTask;

import com.tqjc.common.consts.NormalConst;
import com.tqjc.execute.utils.PlcOperator;
import com.tqjc.system.core.redis.TaskRedisApi;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @projectName: BaotouSteel
 * @className: AccessControlScheduleTask
 * @author: Patienyon
 * @description: 门禁定时任务
 * @date: 2023-07-05 16:25
 */
@Component
public class AccessControlScheduleTask {

    public final static String STOCK_LOCK_PREFIX = "lock_stock_";
    @Resource
    TaskRedisApi taskRedisApi;

    @Resource
    PlcOperator plcOperator;


    /**
     * @param :
     * @return void
     * @author Patienyon
     * @description 根据门禁信号屏蔽垛位
     * @date 2023-07-05 16:35
     */
    //@Scheduled(cron = "0/1 * *  * * ? ")
    public void updateStockLockByPlcSignal() {
        for (Map.Entry<String, Integer> entry : NormalConst.DOOR_POINT_STOCK.entrySet()) {
            boolean isLock = Boolean.parseBoolean(plcOperator.readPoint(entry.getKey()));
            if (isLock) {
                taskRedisApi.set(STOCK_LOCK_PREFIX + entry.getValue().toString(), "true");
            } else {
                taskRedisApi.del(STOCK_LOCK_PREFIX + entry.getValue().toString());
            }
        }

    }

    public boolean getIsStockLockByStockId(Integer stockId) {
        if (taskRedisApi.hasKey(STOCK_LOCK_PREFIX + stockId)) {
            return true;
        }
        return false;

    }


}
